package com.syzg.core.page;

import java.util.ArrayList;
import java.util.Collection;

public class DefaultPageResult
  implements PageResult
{
  private static final long serialVersionUID = 1L;
  private int currentPageNo;
  private int maxPageSize;
  private long totalCount;
  private int totalPageCount;
  private Collection<Object> items;

  public DefaultPageResult()
  {
    this(new ArrayList(), 0L, 0L, 0);
  }

  public DefaultPageResult(Collection<Object> items, long totalCount, long startIndex, int maxPageSize) {
    this.items = new ArrayList();
    this.items = (items != null ? items : new ArrayList());
    this.totalCount = totalCount;
    if (this.totalCount == 0L)
    {
      this.currentPageNo = 0;
      this.maxPageSize = 0;
    } else {
      if (maxPageSize <= 0)
        maxPageSize = 10;
      this.maxPageSize = maxPageSize;
      this.currentPageNo = startIndexToPageNo(startIndex, maxPageSize);
    }
    this.totalPageCount = ((int)((totalCount + maxPageSize - 1L) / maxPageSize));
  }

  public int getCurrentPageNo() {
    return this.currentPageNo;
  }

  public int getMaxPageSize() {
    return this.maxPageSize;
  }

  public long getTotalCount() {
    return this.totalCount;
  }

  public int getTotalPageCount() {
    return this.totalPageCount;
  }

  public Collection<Object> getItems() {
    return this.items;
  }

  public static int startIndexToPageNo(long startIndex, int maxPageSize) {
    if (maxPageSize <= 0) {
      maxPageSize = 10;
    }
    return (int)(startIndex % maxPageSize <= 0L ? startIndex / maxPageSize + 1L : startIndex / maxPageSize + 2L);
  }

  public static long pageNoToStartIndex(long totalCount, int pageNo, int maxPageSize) {
    if ((pageNo <= 1) || (totalCount == 0L)) {
      return 0L;
    }
    int startIndex = (pageNo - 1) * maxPageSize;
    if (startIndex < totalCount) {
      return startIndex;
    }
    return (totalCount - 1L) / maxPageSize * maxPageSize;
  }

  public static int adjustPageNo(long totalCount, int pageNo, int maxPageSize)
  {
    return startIndexToPageNo(pageNoToStartIndex(totalCount, pageNo, maxPageSize), maxPageSize);
  }
}